package queue

import (
	"context"
	"encoding/json"
	redis2 "github.com/redis/go-redis/v9"
	"test/redis"
	"time"
)

/**
 * 消费者
 */
type Consumer struct {}

//func Instance() Consumer {
//	return Consumer{}
//}

func (c *Consumer) Run(job JobInterface) {
	var redisCli = redis.Instance()

	// 根据传进来的消费者信息提取redis中的队列进行消费
	jobTmp := NewQueue(job)
	res, err := redisCli.BRPop(context.Background(), 1*time.Second, jobTmp.QueueName).Result()
	if err == nil {
		// 解析出来
		err = json.Unmarshal([]byte(res[1]), &jobTmp)
		job.GetQueueName()
		jobTmp.Job.Handle(jobTmp.Data)
	} else {
		if err == redis2.Nil {
			// 忽略
		} else {
			// 报错
		}
	}
}